package com.xz.stu.server.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xz.stu.server.entity.Department;
import com.xz.stu.server.entity.StuClass;
import com.xz.stu.server.service.ClassService;
import com.xz.stu.server.mapper.ClassMapper;
import com.xz.stu.server.service.DepartmentService;
import com.xz.stu.server.vo.stuClass.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author 熊磊
* @description 针对表【school_class】的数据库操作Service实现
* @createDate 2025-03-01 15:18:50
*/
@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, StuClass>
    implements ClassService{
    @Resource
    private DepartmentService departmentService;

    @Override
    public Long createClass(ClassCreateReqVO createReqVO) {
        StuClass stuClass = BeanUtils.toBean(createReqVO, StuClass.class);
        this.save(stuClass);
        return stuClass.getId() ;
    }

    @Override
    public void updateClass(ClassUpdateReqVO updateReqVO) {
        StuClass stuClass = BeanUtils.toBean(updateReqVO, StuClass.class);
        this.updateById(stuClass);
    }

    @Override
    public void deleteClass(List<Long> id) {
        this.removeByIds(id);
    }

    @Override
    public PageResult<ClassRespVO> getClassPage(ClassPageReqVO pageVO) {
        Map<Long, String> stringMap = departmentService.list().stream().collect(Collectors.toMap(Department::getId, Department::getDeptName));
        LambdaQueryWrapper<StuClass> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(pageVO.getClassName()), StuClass::getClassName, pageVO.getClassName())
                .like(Objects.nonNull(pageVO.getHeadTeacher()), StuClass::getHeadTeacher, pageVO.getHeadTeacher())
                .eq(Objects.nonNull(pageVO.getDeptId()), StuClass::getDeptId, pageVO.getDeptId());
        Page<StuClass> page = this.page(new Page<>(pageVO.getPageNo(), pageVO.getPageSize()), queryWrapper);
        if (page != null) {
            List<ClassRespVO> classRespVOS = BeanUtils.toBean(page.getRecords(), ClassRespVO.class);
            classRespVOS.forEach(classRespVO -> {
                classRespVO.setDeptName(stringMap.get(classRespVO.getDeptId()));
            });
            return new PageResult<>(classRespVOS, page.getTotal());
        }
        return null;
    }

    @Override
    public List<ClassSimpleRespVO> getClassListByDeptId(Long deptId) {
        Map<Long, String> stringMap = departmentService.list().stream().collect(Collectors.toMap(Department::getId, Department::getDeptName));

        List<StuClass> stuClasses = this.list(new LambdaQueryWrapper<StuClass>().eq(StuClass::getDeptId, deptId));
        if (stuClasses != null) {
            List<ClassSimpleRespVO> list = BeanUtils.toBean(stuClasses, ClassSimpleRespVO.class);
            list.forEach(classSimpleRespVO -> {
                classSimpleRespVO.setDeptName(stringMap.get(classSimpleRespVO.getDeptId()));
            });
            return list;
        }
        return null;
    }

    @Override
    public ClassRespVO getStuClass(Long id) {
        StuClass stuClass = this.getById(id);
        Department department = departmentService.getById(stuClass.getDeptId());
        ClassRespVO classRespVO = BeanUtils.toBean(stuClass, ClassRespVO.class);
        classRespVO.setDeptName(department.getDeptName());
        return classRespVO;
    }
}




